home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6915 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  158 lines

  1. Path: news.nevada.edu!not-for-mail
  2. From: kesslert@nevada.edu (Troy Kessler)
  3. Newsgroups: comp.lang.c++
  4. Subject: Need Help With CSC Homework
  5. Date: 16 Feb 1996 02:42:31 GMT
  6. Organization: University of Nevada System Computing Services
  7. Message-ID: <4g0qun$jp@news.nevada.edu>
  8. NNTP-Posting-Host: unauthenticated_user@pioneer.nevada.edu
  9. X-Newsreader: TIN [UNIX 1.3 950520BETA PL0]
  10.  
  11. pfun.cpp
  12.  
  13. //*************************************************
  14. //**             ROMANFUN.CPP                    **
  15. //*************************************************
  16. //** This program will add, subtract, multiply,  **
  17. //** and divide roman numbers. The numbers are   **
  18. //** assumed to be less than 20 characters long. **
  19. //** The roman numbers should not be inputed in  **
  20. //** subtraction form. Examples - IV=6 IIII=4    **
  21. //*************************************************
  22. #include <iostream.h>   //for cin and cout
  23. #include "p.h"      //has typedef and prototypes
  24. istream& operator>>(istream& in,Romantype& x)
  25. {
  26. in>>x.Roman;
  27. return in;
  28. }
  29.  
  30. ostream& operator<<(ostream& out,Romantype x)
  31. {
  32. out<<x.Roman;
  33. return out;
  34. }
  35.  
  36. Romantype::Romantype()
  37. {
  38. int i;
  39. for(i=1;i<MAXLENGTH;i++)
  40. {
  41. Roman[i]=' ';
  42. }
  43. }
  44.  
  45. //*************************************************
  46. //**                 ADD FUNCTION                **
  47. //*************************************************
  48. //** This function adds two roman numbers. It    **
  49. //** requires a character array and two roman    **
  50. //** numbers.                                    **
  51. //*************************************************
  52. void Romantype::operator+(Romantype numbertwo)
  53. {
  54. int numbero;
  55. int numbert;
  56. int answer;
  57. Romantype answerr;
  58. numbero=RomanToDecimal(Roman);
  59. numbert=RomanToDecimal(numbertwo.Roman);
  60. answer=numbero+numbert;
  61. DecimalToRoman(answerr.Roman,answer);
  62. cout<<answerr.Roman;
  63. }
  64.  
  65. //*************************************************
  66. //**     ROMAN TO DECIMAL FUNCTION               **
  67. //*************************************************
  68. //** This function converts a roman number to a  **
  69. //** decimal number. It requires the character   **
  70. //** array and the number of characters in the   **
  71. //** array.                                      **
  72. //*************************************************
  73. int Romantype::RomanToDecimal(char[MAXLENGTH])
  74. {
  75. int number=0;               //value of number intialized to zero
  76. int i;                      //used in for loop
  77. for(i=1;i<MAXLENGTH;i++)          //until end of array add values of letters
  78. {
  79. switch(Roman[i])
  80. {
  81. case 'I':number=number+1;break;
  82. case 'V':number=number+5;break;
  83. case 'X':number=number+10;break;
  84. case 'L':number=number+50;break;
  85. case 'C':number=number+100;break;
  86. case 'D':number=number+500;break;
  87. case 'M':number=number+1000;break;
  88. }
  89. }
  90. return number;    //return decimal value to where the function was called
  91. }
  92.  
  93. //*************************************************
  94. //**     DECIMAL TO ROMAN FUNCTION               **
  95. //*************************************************
  96. //** This function converts a decimal number to  **
  97. //** a roman number. It requires a character     **
  98. //** array and the value of the decimal number.  **
  99. //*************************************************
  100. void Romantype::DecimalToRoman(char[MAXLENGTH],int answer)
  101. {
  102. int i=1;           //used in for loop
  103. while(answer-1000>=0) //convert to roman by storing letters in array
  104. {
  105. answerr.Roman[i]='M';
  106. answer=answer-1000;
  107. i++;
  108. }
  109. while(answer-500>=0)
  110. {
  111. answerr.Roman[i]='D';
  112. answer=answer-500;
  113. i++;
  114. }
  115. while(answer-100>=0)
  116. {
  117. answerr.Roman[i]='C';
  118. answer=answer-100;
  119. i++;
  120. }
  121. while(answer-50>=0)
  122. {
  123. answerr.Roman[i]='L';
  124. answer=answer-50;
  125. i++;
  126. }
  127. while(answer-10>=0)
  128. {
  129. answerr.Roman[i]='X';
  130. answer=answer-10;
  131. i++;
  132. }
  133. while(answer-5>=0)
  134. {
  135. answerr.Roman[i]='V';
  136. answer=answer-5;
  137. i++;
  138. }
  139. while(answer-1>=0)
  140. {
  141. answerr.Roman[i]='I';
  142. answer=answer-1;
  143. i++;
  144. }
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.